package com.tledu.email.service.impl;

import java.util.List;

import com.tledu.common.core.domain.entity.SysUser;
import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tledu.email.mapper.EmailDraftMapper;
import com.tledu.email.domain.EmailDraft;
import com.tledu.email.service.IEmailDraftService;

/**
 * 草稿箱Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@Service
public class EmailDraftServiceImpl implements IEmailDraftService {
    @Autowired
    private EmailDraftMapper emailDraftMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询草稿箱
     *
     * @param id 草稿箱主键
     * @return 草稿箱
     */
    @Override
    public EmailDraft selectEmailDraftById(Long id) {
        return emailDraftMapper.selectEmailDraftById(id);
    }

    /**
     * 查询草稿箱列表
     *
     * @param emailDraft 草稿箱
     * @return 草稿箱
     */
    @Override
    public List<EmailDraft> selectEmailDraftList(EmailDraft emailDraft) {
        return emailDraftMapper.selectEmailDraftList(emailDraft);
    }

    /**
     * 新增草稿箱
     *
     * @param emailDraft 草稿箱
     * @return 结果
     */
    @Override
    public int insertEmailDraft(EmailDraft emailDraft) {
        emailDraft.setCreateTime(DateUtils.getNowDate());
        return emailDraftMapper.insertEmailDraft(emailDraft);
    }

    /**
     * 修改草稿箱
     *
     * @param emailDraft 草稿箱
     * @return 结果
     */
    @Override
    public int updateEmailDraft(EmailDraft emailDraft) {
        emailDraft.setUpdateTime(DateUtils.getNowDate());
        return emailDraftMapper.updateEmailDraft(emailDraft);
    }

    /**
     * 批量删除草稿箱
     *
     * @param ids 需要删除的草稿箱主键
     * @return 结果
     */
    @Override
    public int deleteEmailDraftByIds(Long[] ids) {
        return emailDraftMapper.deleteEmailDraftByIds(ids);
    }

    /**
     * 删除草稿箱信息
     *
     * @param id 草稿箱主键
     * @return 结果
     */
    @Override
    public int deleteEmailDraftById(Long id) {
        return emailDraftMapper.deleteEmailDraftById(id);
    }

    // 查询当前用户的草稿箱列表
    @Override
    public List<EmailDraft> selectUserDraftsList(EmailDraft emailDraft) {
        // 获得收件人名称
        // Long recipientsId = emailDraft.getRecipientsId();
        // SysUser user = userService.selectUserById(recipientsId);
        // String userName = user.getUserName();
        // emailDraft.setRecipientsName(userName);
        Long userId = SecurityUtils.getUserId();
        emailDraft.setCreateId(userId);
        return emailDraftMapper.selectUserDraftsList(emailDraft);
    }

    // 根据条件筛选当前用户的草稿箱列表 selectDraftsByConditionList
    @Override
    public List<EmailDraft> selectDraftsByConditionList(EmailDraft emailDraft) {
        // 当前发件人
        Long userId = SecurityUtils.getUserId();
        emailDraft.setCreateId(userId);
        return emailDraftMapper.selectDraftsByConditionList(emailDraft);
    }

    // 草稿箱逻辑删除
    @Override
    public int updateSymbol(Long[] ids) {
        return emailDraftMapper.updateSymbol(ids);
    }
}
